package com.tomato.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tomato.order.VO.CategoryProductVO;
import com.tomato.order.VO.ProductRatingVO;
import com.tomato.order.VO.ResultVO;
import com.tomato.order.VO.SellerInfo;
import com.tomato.order.entity.*;
import com.tomato.order.mapper.*;
import com.tomato.order.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ClientController {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private RatingMapper ratingMapper;
    @Resource
    private SellerMapper sellerMapper;
    @Resource
    private InfoMapper infoMapper;
    @Resource
    private ImageMapper imageMapper;
    @Resource
    private SupportMapper supportMapper;

    //    @GetMapping("/goods")
//    public List<CategoryProductVO> drugs() {
//        List<Category> categoryList = categoryMapper.selectList(null);//获取所有类目
//        List<CategoryProductVO> categoryProductVOList = new ArrayList<CategoryProductVO>();
//        for (int i = 0; i < categoryList.size(); i++) {//封装 categoryProductVOList
//            List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().
//                    eq("type", categoryList.get(i).getType()).
//                    ne("stock", 0).
//                    eq("status", 1));//获取当前类目所有产品
//            List<ProductRatingVO> productRatingVOList = new ArrayList<ProductRatingVO>();
//            for (int j = 0; j < productList.size(); j++) {//封装 productRatingVOList
//                List<Rating> ratingList = ratingMapper.selectList(new QueryWrapper<Rating>().
//                        eq("product_id", productList.get(i).getId()));
//                ProductRatingVO productRatingVO = new ProductRatingVO();//创建产品VO
//                BeanUtils.copyProperties(productList.get(j), productRatingVO);//复制产品数据到VO
//                productRatingVO.setRatings(ratingList);
//                productRatingVOList.add(productRatingVO);
//            }
//            CategoryProductVO categoryProductVO = new CategoryProductVO();//创建类目VO
//            BeanUtils.copyProperties(categoryList.get(i), categoryProductVO);//复制类目数据到VO
//            categoryProductVO.setFoods(productRatingVOList);//
//            categoryProductVOList.add(categoryProductVO);
//        }
//        return categoryProductVOList;
//    }
    @GetMapping("/goods")
    public List<CategoryProductVO> drugs() {
        List<Category> categoryList = categoryMapper.selectList(null);//获取所有类目
        List<CategoryProductVO> categoryProductVOList = new ArrayList<CategoryProductVO>();
        for (int i = 0; i < categoryList.size(); i++) {//封装 categoryProductVOList
            List<Product> productList = productMapper.selectList(new QueryWrapper<Product>().
                    eq("type", categoryList.get(i).getType()).
                    ne("stock", 0).
                    eq("status", 1));//获取当前类目所有产品
            List<ProductRatingVO> productRatingVOList = new ArrayList<ProductRatingVO>();
            for (int j = 0; j < productList.size(); j++) {//封装 productRatingVOList
                ProductRatingVO productRatingVO = new ProductRatingVO();//创建产品VO
                BeanUtils.copyProperties(productList.get(j), productRatingVO);//复制产品数据到VO
                productRatingVOList.add(productRatingVO);
            }
            CategoryProductVO categoryProductVO = new CategoryProductVO();//创建类目VO
            BeanUtils.copyProperties(categoryList.get(i), categoryProductVO);//复制类目数据到VO
            categoryProductVO.setFoods(productRatingVOList);//
            categoryProductVOList.add(categoryProductVO);
        }
        return categoryProductVOList;
    }

    @GetMapping("/seller")
    public SellerInfo seller(@RequestParam("id") String id) {
        Seller seller = sellerMapper.selectOne(new QueryWrapper<Seller>().eq("id", id));
        SellerInfo sellerInfo = new SellerInfo();
        BeanUtils.copyProperties(seller, sellerInfo);
        sellerInfo.setInfos(infoMapper.selectList(new QueryWrapper<Info>().eq("other", seller.getInfos())));
        sellerInfo.setSupports(supportMapper.selectList(new QueryWrapper<Support>().eq("seller_id", seller.getSupports())));
        sellerInfo.setPics(imageMapper.selectList(new QueryWrapper<Image>().eq("other", seller.getPics())));
        return sellerInfo;
    }

    @GetMapping("/ratings")
    public List ratings(@RequestParam(value = "productId", defaultValue = "0") String productId) {
        return ratingMapper.selectList(new QueryWrapper<Rating>().
                eq("product_id", productId).
                orderByDesc("rate_time"));
    }

    @GetMapping("/rating/send")
    public ResultVO sendRating(Rating rating) {
        int result = ratingMapper.insert(rating);
        if (result == 1) {
            return R.success();
        } else {
            return R.error(1, "评价失败");
        }
    }
}
